package com.hyyt.shopjsp.statistics.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.export.ExportExcel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.vo.ShopShopInfoVo;
import com.hyyt.shopjsp.store.info.service.IShopShopInfoService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * @author 刘超 on 2017/7/17.
 */

@Controller
@RequestMapping("/back/shopSalesStatistics")
public class ShopSalesStatisticsController extends BaseController {

    private Logger log = Logger.getLogger(ShopSalesStatisticsController.class);
    private String basePath = "/back/shop/statistics/";

    @Resource
    private IShopShopInfoService shopShopInfoService;//供应商商城币收支明细service

    /**
     * 跳转店铺销售排行统计页面
     *
     * @return 返回到指定页面
     */
    @RequestMapping("gotoShopSalesStatistics")
    public String gotoShopSalesStatistics(){
        return basePath + "shopSalesStatisticsList";
    }

    /**
     * 店铺销售排行统计列表
     * @param page
     * @param response
     * @param createTimeStart   开始时间
     * @param createTimeEnd     结束时间
     * @param shopName          店铺名称
     */
    @RequestMapping(value="listShopSalesStatistics",method= RequestMethod.POST)
    @ResponseBody
    public void listShopSalesStatistics(Page page,String createTimeStart,String createTimeEnd,String shopName,HttpServletResponse response){
        Map<String, Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(shopName)){
            params.put("shopName", shopName.trim());
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        params.put("sortColumns", "count desc");
        int totalRecordCount =shopShopInfoService.countShopShopInfo(params);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<ShopShopInfoVo> shopShopInfoList=shopShopInfoService.queryShopSalesStatistics(params, page.getPageRecordBeginIndex(), page.getPageSize());
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", shopShopInfoList);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面
    }

    /**
     * 店铺销售排行统计列表
     * @param createTimeStart   开始时间
     * @param createTimeEnd     结束时间
     * @param shopName          店铺名称
     * @param request
     * @param response
     */
    @RequestMapping("exportShopSalesStatisticsExcel")
    public void exportShopSalesStatisticsExcel(String createTimeStart, String createTimeEnd, String shopName, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> params=new HashMap<>();
        if(Utils.objectIsNotEmpty(shopName)){
            params.put("shopName", shopName.trim());
        }
        if(Utils.objectIsNotEmpty(createTimeStart)){
            params.put("createTimeStart", createTimeStart);
        }
        if(Utils.objectIsNotEmpty(createTimeEnd)){
            params.put("createTimeEnd", createTimeEnd);
        }
        params.put("sortColumns", "count desc");
        List<ShopShopInfoVo> shopShopInfoList=shopShopInfoService.queryShopSalesStatistics(params);
        SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
        List<Map<String,Object>> shopSalesStatistics = new ArrayList<>();
        if(shopShopInfoList!=null&&shopShopInfoList.size()>0){
            for (ShopShopInfoVo shopShopInfoVo : shopShopInfoList) {
                Map<String,Object> map = new HashMap<>();
                map.put("logoUrl", shopShopInfoVo.getLogoUrl());
                map.put("shopName", shopShopInfoVo.getShopName());
                if(Utils.objectIsNotEmpty(shopShopInfoVo.getCount())){
                    map.put("count", shopShopInfoVo.getCount());
                }else{
                    map.put("count","0");
                }
                shopSalesStatistics.add(map);
            }
        }
        String fileName ="店铺销售排行统计";
        String columnNames[] = {"店铺LOGO","店铺名称","订单数量（单）"};
        String keys[] = {"logoUrl","shopName","count"};
        try {
            ExportExcel.download(fileName,shopSalesStatistics,columnNames,keys,request,response);
        } catch (IOException e) {
            logger.error("导出Excel失败！");
        }


    }

}
